<template>
  <div class="musicPlayList">
    <div
      class="musicPlayList-con"
      v-for="(item, index) in MusicListData"
      :key="item.id"
      @click.stop="getURL(item.id)"
      :class="item.id === musicID ? 'musicPlayList-Active' : ''"
    >
      <div class="con-index fl">{{ index + 1 }}</div>
      <div class="con-name fl">
        <div class="name">{{ MusicListData.length > 0 ? item.name : "" }}</div>
        <div class="singer">
          {{ MusicListData.length > 0 ? item.ar[0].name : "" }}
        </div>
      </div>
      <div class="con-list fr">
        <div class="con-list-item item0">
          <el-tooltip
            class="item"
            effect="dark"
            content="加入喜欢列表"
            placement="top"
          >
            <img
              class="auto-img"
              :src="require('../../assets/image/icon/shoucang_1.png')"
              alt=""
            />
          </el-tooltip>
        </div>
        <div class="con-list-item item1">
          <el-tooltip
            class="item"
            effect="dark"
            content="加入歌单"
            placement="top"
          >
            <img
              class="auto-img"
              :src="require('../../assets/image/icon/jiahao.png')"
              alt=""
            />
          </el-tooltip>
        </div>

        <div
          class="con-list-item item3"
          @click.stop="downloadMusic(item.id, item.name)"
        >
          <DownLoad ref="mychild" />
          <el-tooltip class="item" effect="dark" content="下载" placement="top">
            <img
              class="auto-img"
              :src="require('../../assets/image/icon/xiazai.png')"
              alt=""
            />
          </el-tooltip>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getMusicDetailInfo, getURLInfo } from "@/Api/seachApi.js";

import DownLoad from "@/components/publicCompontents/DownLoad";
export default {
  name: "MusicPlayList",
  components: {
    DownLoad,
  },
  data() {
    return {
      // 列表数据
      MusicListData: [],
      timer: null,
    };
  },
  computed: {
    musicPlayListData() {
      return this.$store.state.musicState.musicPlayListData;
    },
    // 获取audio标签
    getAudioElement() {
      return this.$store.state.musicState.audioElement;
    },
    // 音乐Id
    musicID() {
      return this.$store.state.musicState.musicID;
    },
  },
  created() {
    // console.log(this.musicPlayList);
    this.getOneMusicDetail();
  },
  methods: {
    // 获取点击的歌曲详情
    getOneMusicDetail() {
      // console.log("参数出错",this.musicPlayListData);
      getMusicDetailInfo(this.musicPlayListData)
        .then((res) => {
          if (res.code === 200) {
            this.MusicListData = res.songs;
          }
        })
        .catch((err) => {
          // loading.close();
          // console.log(err);
        });
    },
    // 点击播放
    getURL(id) {
      // console.log(id);
      this.$store.commit("musicState/isClickPlay", false);
      getURLInfo({ id: id })
        .then((res) => {
          // console.log(res);
          // console.log(1)
          if (res.code == 200) {
            // this.$store.commit("musicState/audioSrc", res.data[0].url);
            this.$store.commit("musicState/SetMusicID", id);
            // setTimeout(() => {
            //   this.$store.commit("musicState/isClickPlay", true);
            //   this.getAudioElement.play();
            //   this.$store.commit("musicState/isMusicPlayList", false);
            // }, 800);
          }
        })
        .catch((err) => {
          // console.log(err);
        });
    },
    // 下载音乐
    downloadMusic(uid, MusicName) {
      this.$nextTick(() => {
        // console.log("列表", this.$refs.mychild);
        // debugger
        this.$refs.mychild[0].downLoad(uid, MusicName);
      });
    },
  },
  watch: {
    musicPlayListData: function (newVal) {
      this.getOneMusicDetail();
    },
  },
};
</script>

<style lang="scss" scoped>
.musicPlayList {
  width: 500px;
  height: 500px;
  z-index: 200;
  background-color: rgba(199, 202, 204, 0.9);
  // background-color: #ccc;
  border-radius: 20px;
  overflow: hidden;
  overflow: scroll;
  // 滚动不产生滚动条兼容
  &::-webkit-scrollbar {
    width: 0px;
  }
  &::-o-scrollbar {
    width: 0px;
  }
  &::-moz-scrollbar {
    width: 0px;
  }

  .musicPlayList-con {
    width: 100%;
    height: 50px;
    margin-top: 10px;
    background-color: rgba(255, 255, 255, 0.1);

    &:hover {
      background-color: rgba(0, 0, 0, 0.2);
    }
  }
  .musicPlayList-Active {
    background-color: rgba(0, 0, 0, 0.2) !important;
  }
  .con-index {
    width: 50px;
    height: 100%;
    line-height: 60px;
    font-size: 28px;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.6);
  }
  .con-name {
    height: 100%;
    // width: 200px;
    margin-left: 20px;
    font-size: 16px;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.6);
    // background-color: red;
    float: left;
    display: flex;
    flex-flow: column;
    text-align: left;

    .name {
      font-size: 18px;
      margin-bottom: 3px;
      max-width: 300px;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }
  .con-list {
    height: 100%;
    line-height: 60px;
    float: right;
    margin-right: 20px;

    .con-list-item {
      display: inline-block;
      width: 26px;
      height: 26px;

      vertical-align: middle;
      margin-right: 10px;
      cursor: pointer;
    }
  }
}
</style>
